﻿using FluentValidation;
using MXERP.Infrastructure.Managers;

namespace MXERP.Client.Pages
{
    public class RoleFluentValidator: AbstractValidator<RoleResponse>
    {
        public RoleFluentValidator() 
        {
            RuleFor(x => x.Name)
                .NotEmpty().WithMessage("请输入角色名称")
                .MaximumLength(50).WithMessage("不能多于50个字符");
        }
        public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
        {
            var result = await ValidateAsync(ValidationContext<RoleResponse>.CreateWithOptions((RoleResponse)model, x => x.IncludeProperties(propertyName)));
            if (result.IsValid)
                return Array.Empty<string>();
            return result.Errors.Select(e => e.ErrorMessage);
        };
    }
}
